package su.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.user.dao.IUserYwyDAO;
import su.user.entity.UserYwy;


@Repository
public class UserYwyDAO extends SuBaseDAO implements IUserYwyDAO {

	public List<UserYwy> queryByPage(PagerDTO s)throws SQLException{
		String sql = "select * from su_user_ywy where status=1";
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql += " and id in ("+s.getS1()+")";
			}else{
				sql += " and id="+s.getS1();
			}
		}
		if(StringUtils.isNotBlank(s.getS2())){//简拼
			sql += " and jianpin like '%"+s.getS2()+"%'";
		}
		sql += " ORDER BY id desc";
		
		return super.queryByPage(sql, new RowMapper<UserYwy>() {

			public UserYwy mapRow(ResultSet rs, int arg1) throws SQLException {
				UserYwy entity = new UserYwy();
				entity.setId(rs.getInt("id"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setYwydesc(rs.getString("ywydesc"));
				entity.setYwyname(rs.getString("ywyname"));
				return entity;
			}
			
		}, s);
	}
	
	public UserYwy getById(int id)throws SQLException{
		String sql = "select * from su_user_ywy where status=1 and id=?";
		return super.queryForObject(sql,new Object[]{id},new RowMapper<UserYwy>(){
			public UserYwy mapRow(ResultSet rs, int arg1) throws SQLException {
				UserYwy entity = new UserYwy();
				entity.setId(rs.getInt("id"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setYwydesc(rs.getString("ywydesc"));
				entity.setYwyname(rs.getString("ywyname"));
				return entity;
			}
			
		});
	}
	
	public List<UserYwy> getAllYwy()throws SQLException{
		String sql = "select * from su_user_ywy where status=1 order by id";
		return super.query(sql, new RowMapper<UserYwy>() {

			public UserYwy mapRow(ResultSet rs, int arg1) throws SQLException {
				UserYwy entity = new UserYwy();
				entity.setId(rs.getInt("id"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setStatus(rs.getInt("status"));
				entity.setYwydesc(rs.getString("ywydesc"));
				entity.setYwyname(rs.getString("ywyname"));
				return entity;
			}
			
		});
	}
	
	public void insert(UserYwy o)throws SQLException{
		String sql = "insert into su_user_ywy(jianpin,status,ywydesc,ywyname) values(?,?,?,?)";
		super.update(sql, new Object[]{o.getJianpin(),o.getStatus(),o.getYwydesc(),o.getYwyname()});
	}
	
	public void update(UserYwy o)throws SQLException{
		String sql = "update su_user_ywy set jianpin=?,ywydesc=?,ywyname=? where id=?";
		super.update(sql, new Object[]{o.getJianpin(),o.getYwydesc(),o.getYwyname(),o.getId()});
	}

	public void remove(int id)throws SQLException{
		String sql = "update su_user_ywy set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
}

